// miniprogram/pages/prizeTest/prizeTest.js


//item模板
const mAwardItem = {
  topAward: 10,
  leftAward: 10,
  imageAward: 'http://bpic.588ku.com/element_origin_min_pic/17/08/14/26b959a5092c18d60421561bd74ac27c.jpg'
}

Page({

  data: {
    topMargin: 20,
    leftMargin: 20,
    colorCircleFirst: '',
    colorCircleSecond: '',
    circleList: [{
      topCircle: 1,
      leftCircle: 1
    }],
    awardList: [],
    indexSelect: 0,
    isRunning: false
  },
  onLoad: function (options) {
    var topMargin = this.data.topMargin
    var leftMargin = this.data.leftMargin
    var awardList = this.data.awardList
    for (let i = 0; i < 8; i++) {
      var mAwardItemStr = JSON.stringify(mAwardItem)
      var awardItem = JSON.parse(mAwardItemStr)
      switch (i) {
        case 0:
          awardItem.topAward = topMargin
          awardItem.leftAward = leftMargin
          break
        case 1:
          awardItem.topAward = topMargin
          awardItem.leftAward = leftMargin + leftMargin + 166
          break
        case 2:
          awardItem.topAward = topMargin
          awardItem.leftAward = leftMargin + leftMargin * 2 + 166 * 2
          break
        case 3:
          awardItem.topAward = topMargin + topMargin * 1 + 150
          awardItem.leftAward = leftMargin + leftMargin * 2 + 166 * 2
          break
        case 4:
          awardItem.topAward = topMargin + topMargin * 2 + 150 * 2
          awardItem.leftAward = leftMargin + leftMargin * 2 + 166 * 2
          break
        case 5:
          awardItem.topAward = topMargin + topMargin * 2 + 150 * 2
          awardItem.leftAward = leftMargin + leftMargin * 1 + 166 * 1
          break
        case 6:
          awardItem.topAward = topMargin + topMargin * 2 + 150 * 2
          awardItem.leftAward = leftMargin
          break
        case 7:
          awardItem.topAward = topMargin + topMargin * 1 + 150 * 1
          awardItem.leftAward = leftMargin
          break
      }
      console.log("awardItem", JSON.stringify(awardItem))
      awardList.push(awardItem)
    }
    this.setData({
      awardList: awardList
    })
  },
  
  //开始抽奖
  startGame: function () {
    if (this.data.isRunning) return
    this.setData({
      isRunning: true
    })
    var _this = this;
    var indexSelect = 0
    var i = 0;
    var timer = setInterval(function () {
      indexSelect++;
      //这里我只是简单粗暴用y=30*x+200函数做的处理.可根据自己的需求改变转盘速度
      i += 30;
      if (i > 1000) {
        //去除循环
        clearInterval(timer)
        //获奖提示
        wx.showModal({
          title: '恭喜您',
          content: '获得了第' + (_this.data.indexSelect + 1) + "个优惠券",
          showCancel: false, //去掉取消按钮
          success: function (res) {
            if (res.confirm) {
              _this.setData({
                isRunning: false
              })
            }
          }
        })
      }
      indexSelect = indexSelect % 8;
      _this.setData({
        indexSelect: indexSelect
      })
    }, (200 + i))
  }
})